/*
* @(#)MaintHolidayRecordAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.setup.web;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.client.analyzer.DmsDocumentAnalyzer;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.setup.dao.WorkingCalendarDAObject;

/**
 * MaintHolidayRecordAction.java
 * 
 * This class is for maint dms dmsDocument action
 * 
 * @author Weison Liang
 * @company DCIVision Limited
 * @creation date 30/03/2005
 * @version $Revision: 1.11.2.1 $
 */

public class MaintHolidayRecordAction extends AbstractMaintAction  {

  public static final String REVISION = "$Revision: 1.11.2.1 $";
  

  protected static String MESSAGE_DUPLICATE_NAME = "setup.message.duplicate_name";
  protected static String MESSAGE_DATE_OVERLAP = "setup.message.overlap";

  protected static String scannedUDFName = "";
  protected String parentID = "";
  protected String rootID = "";
  protected String docType = "";
  protected DmsDocumentAnalyzer documentAnalyzer = null;

  private String odmaProcess = "";  
  
  public MaintHolidayRecordAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.setup.dao.HolidayRecordDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.SETUP_HOLIDAY_RECORD);
  }

  /**
   * execute - Override the parent function.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
   
    //get the working calendar list.
    this.getWorkingCalendarList(request);
        
    String opMode = actionForm.getOpMode();
    String navMode = actionForm.getNavMode();    
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    if(GlobalConstant.NAV_MODE_REFRESH.equals(opMode)){
      actionForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
      return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
    }
      return super.execute(mapping, form, request, response);
    
    
  }

  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    Timestamp endDateTimestamp = Utility.parseDateStringToSystemDate(actionForm.getEndDate(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));
    Calendar endDateCal = Utility.timestampToCalendar(endDateTimestamp);
    endDateCal.set(Calendar.HOUR_OF_DAY, 23);
    endDateCal.set(Calendar.MINUTE, 59);
    endDateCal.set(Calendar.SECOND, 59);
    endDateCal.set(Calendar.MILLISECOND, 0);
    endDateTimestamp = Utility.calendarToTimestamp(endDateCal);
    actionForm.setEndDate(endDateTimestamp.toString());
    super.insertRecord(mapping, form, request, response);
/*    
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    HolidayRecordDAObject holidayRecordDAO = new HolidayRecordDAObject(sessionContainer,conn);
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    HolidayRecord holidayRecord = (HolidayRecord)actionForm.getFormData();
    String endDate=actionForm.getEndDate().substring(0,11)+"23:59:59";
    
    boolean bDuplicateName = this.checkDuplicateHolidayName(mapping,form,request,response);
    boolean bDuplicateDate = this.checkDuplicateDate(mapping,form,request,response);
    
    if(!bDuplicateName && !bDuplicateDate){
      holidayRecord.setWorkingCalendarID(new Integer(actionForm.getWorkingCalendarID()));
      holidayRecord.setHolidayName(actionForm.getHolidayName());
      if("1".equals(actionForm.getReferenceFlag())){
        holidayRecord.setReferenceFlag("1");
        holidayRecord.setStartDate(this.parseTimestamp(actionForm.getStartDate()));
        holidayRecord.setEndDate(this.parseTimestamp(endDate));
      }else if("0".equals(actionForm.getReferenceFlag())){
        holidayRecord.setReferenceFlag("0");	
        holidayRecord.setStartDate(this.parseTimestamp(actionForm.getStartDate()));
        holidayRecord.setEndDate(this.parseTimestamp(endDate));
      }
      holidayRecord.setHolidayType(TextUtility.parseIntegerObj(actionForm.getHolidayType()));
        
      holidayRecord = (HolidayRecord)holidayRecordDAO.insertObject(holidayRecord);
      this.commit(request);
      actionForm.setFormData(holidayRecord);
   }else  if(bDuplicateName){
     throw new ApplicationException(MESSAGE_DUPLICATE_NAME);
   }else if(bDuplicateDate){
   	 throw new ApplicationException(MESSAGE_DATE_OVERLAP);
   }
*/    
    
  }

  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    Timestamp endDateTimestamp = Utility.parseDateStringToSystemDate(actionForm.getEndDate(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));
    Calendar endDateCal = Utility.timestampToCalendar(endDateTimestamp);
    endDateCal.set(Calendar.HOUR_OF_DAY, 23);
    endDateCal.set(Calendar.MINUTE, 59);
    endDateCal.set(Calendar.SECOND, 59);
    endDateCal.set(Calendar.MILLISECOND, 0);
    
    endDateTimestamp = Utility.calendarToTimestamp(endDateCal);
    actionForm.setEndDate(endDateTimestamp.toString());
    super.updateRecord(mapping, form, request, response);
    /*  
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    HolidayRecordDAObject holidayRecordDAO = new HolidayRecordDAObject(sessionContainer,conn);
    HolidayRecord holidayRecord = (HolidayRecord)actionForm.getFormData();
    if("1".equals(actionForm.getReferenceFlag())){
        holidayRecord.setReferenceFlag("1");
      }else if("0".equals(actionForm.getReferenceFlag())){
        holidayRecord.setReferenceFlag("0");
      }
    holidayRecord = (HolidayRecord)holidayRecordDAO.updateObject(holidayRecord);
    this.commit(request);
    //actionForm.setFormData(holidayRecord);
*/  
  }

/*
  public void deleteRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {

  }
*/  
  
/*
  public boolean checkDuplicateHolidayName(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response)throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    HolidayRecordDAObject holidayRecordDAO = new HolidayRecordDAObject(sessionContainer,conn);
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    boolean bDuplicateName=false;
    String startDate=actionForm.getStartDate().substring(0,4)+"-01-01 00:00:00";
    String endDate=actionForm.getEndDate().substring(0,4)+"-12-31 23:59:59";
    List holidayList = holidayRecordDAO.getHolidayNameListBydate(new Integer(0),startDate,endDate);
    if(!Utility.isEmpty(holidayList)){
      for(int i=0;i<holidayList.size();i++){
        HolidayRecord holidayRecord = (HolidayRecord)holidayList.get(i);
        if(actionForm.getHolidayName().trim().equals(holidayRecord.getHolidayName())){
          bDuplicateName=true;
          break;
        }else{
          bDuplicateName=false;
        }
      }
    }
    
    return bDuplicateName;
  }
  
  public boolean checkDuplicateDate(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response)throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    HolidayRecordDAObject holidayRecordDAO = new HolidayRecordDAObject(sessionContainer,conn);
    MaintHolidayRecordForm actionForm = (MaintHolidayRecordForm) form;
    boolean bDuplicateDate=false;
    List holidayList = holidayRecordDAO.getHolidayNameListByCalendarIDdate(new Integer(0));
    if(!Utility.isEmpty(holidayList)){
      for(int i=0;i<holidayList.size();i++){
        HolidayRecord holidayRecord = (HolidayRecord)holidayList.get(i);
        boolean bStartDate=this.parseTimestamp(actionForm.getStartDate()).before(holidayRecord.getEndDate());
        boolean bSdate=this.parseTimestamp(actionForm.getStartDate()).after(holidayRecord.getStartDate());
        boolean bEndDate=this.parseTimestamp(actionForm.getEndDate()).after(holidayRecord.getStartDate());
        boolean bEdate=this.parseTimestamp(actionForm.getEndDate()).before(holidayRecord.getEndDate());
        if((bStartDate && bSdate) || (bEdate && bEndDate)){
          bDuplicateDate=true;
          break;
        }else{
          bDuplicateDate=false;
        }
      }
    }
    
    return bDuplicateDate;
  }
  
  public SimpleDateFormat getDateTimeFormat() {
    return new SimpleDateFormat(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));
  }
  
  public SimpleDateFormat getDateFormat() {
    return new SimpleDateFormat(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));
  }
  
  public java.sql.Timestamp parseTimestamp(String timeStr) {
    if (timeStr==null) {
      return null;
    }
    try {
      java.util.Date date;
      try {
        date = getDateTimeFormat().parse(timeStr);
      } catch (java.text.ParseException e1) {
        date = getDateFormat().parse(timeStr);
      }
      return new java.sql.Timestamp(date.getTime());
    } catch (java.text.ParseException e) {
      return null;
    }
  }
*/
  /**
   * get the working calendar list.
   * @param request
   * @throws ApplicationException
   */
  private void getWorkingCalendarList(HttpServletRequest request) throws ApplicationException {
    WorkingCalendarDAObject workingCalDAO = new WorkingCalendarDAObject(this.getSessionContainer(request), this.getConnection(request));
    List workingCalList = workingCalDAO.getFullList();
    request.setAttribute("WORKING_CALENDAR_LIST", workingCalList);    
  }
}
